class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        unordered_map<string,int> hashword;
        vector<int> ret;
        for(auto a:words)
        {
            hashword[a]++;
        }
        int len=words[0].size();
        int mem=words.size();
        for(int i=0;i<len;++i)
        {
            unordered_map<string,int> hashs;
            for(int left=i,right=i,count=0;
            right+len<=s.size();right+=len)
            {
                string in=s.substr(right,len);
                hashs[in]++;
                if(hashs[in]<=hashword[in])
                {
                    count++;
                }
                if(right-left+len>(len*mem))
                {
                    string out=s.substr(left,len);
                    if(hashs[out]<=hashword[out])
                    {
                        count--;
                    }
                    hashs[out]--;
                    left+=len;
                }
                if(count==mem)
                {
                    ret.push_back(left);
                }
            }
        }
        return ret;
    }
};
